/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package gradius.InterfaceGrafica;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JPanel;

/** Classe que cria o Ranking do jogo
 *
 * @author Eduardo
 */
public class Ranking extends JPanel
{
    private Image fundo;
    private ArrayList<String> ranking = new ArrayList<String>();
    private JPanel painel;
    private JButton botao;
    private Janela3 frame;

    /** Construtor da classe Ranking
     *
     * @param frame - Frame da janela 3, correspondente ao ranking
     */
    public Ranking(Janela3 frame)
    {
        this.frame = frame;
        botao = new JButton("Voltar");
        botao.addActionListener(new OuvinteBotao());
        painel = new JPanel(new FlowLayout());
        painel.add(botao);
        painel.setOpaque(false);
        this.setLayout(new BorderLayout());
        this.add(painel,BorderLayout.SOUTH);
        String caminho = "/images/backgrounds/ranking.png";
        URL url = PainelMenu.class.getResource(caminho);
        fundo = Toolkit.getDefaultToolkit().getImage(url);
        caminho = "/ranking/ranking.txt";
        url = PainelMenu.class.getResource(caminho);
        FileReader reader = null;
        try {
            reader = new FileReader(new File(url.getPath()));
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Ranking.class.getName()).log(Level.SEVERE, null, ex);
        }
        BufferedReader breader = new BufferedReader(reader);
        String linha;
        for (int i = 0 ; i<5 ; i++)
        {
            try {
                linha = breader.readLine();
                ranking.add(linha);
            } catch (IOException ex) {
                Logger.getLogger(Ranking.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        try 
        {
            breader.close();
            reader.close();
        } catch (IOException ex) {
            Logger.getLogger(Ranking.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** Método que desenha os elementos na tela
     * @param g - Objeto Graphics para desenho
     */
    @Override
    protected void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D)g;
        g2.drawImage(fundo,0,0, this);
        g2.setColor(Color.white);
        for (int i = 0 ; i<5 ; i++)
        {
            g2.drawString((i+1) + " - " + ranking.get(i), 20, 100 + 20*i);
        }
    }

    /** Classe Ouvinte do envento de clique de botão de ranking
     *
     */
    public class OuvinteBotao implements ActionListener
    {
        /** Método responsavel pela resposta ao evento de clique selecionado
         *
         * @param e - Objeto do evento capturado no clique do botão
         */
        public void actionPerformed(ActionEvent e)
        {
            frame.dispose();
        }
    }

}
